class KRouter {
    constructor(options) {
      this.$options = options;
      Vue.util.defineReactive(
        this,
        "current",
        window.location.hash.slice(1) || "/"
      );
      // set接收的obj必须是响应式的
      // Vue.set(this, "current", window.location.hash.slice(1) || "/");
      window.addEventListener("hashchange", () => {
        this.current = window.location.hash.slice(1);
      });
    }
  }
  let Vue;
  KRouter.install = function(_Vue) {
    Vue = _Vue;
    Vue.mixin({
      beforeCreate() {
        if (this.$options.router) {
          Vue.prototype.$router = this.$options.router;
        }
      },
    });
    Vue.component("router-link", {
      props: {
        to: {
          type: String,
          required: true,
          default: "/",
        },
      },
      render(h) {
        return h("a", { attrs: { href: "#" + this.to } }, this.$slots.default);
      },
    });
    Vue.component("router-view", {
      render(h) {
        let component = null;
        const route = this.$router.$options.routes.find(
          (route) => route.path === this.$router.current
        );
        if (route) component = route.component;
        return h(component);
      },
    });
  };
  export default KRouter